//
//  MGModalView.m
//  Mango
//
//  Created by 胡超 on 2017/11/29.
//  Copyright © 2017年 胡超., ltd. All rights reserved.
//

#import "MGModalView.h"
#import "UIView+Mango.h"

@interface MGModalView () {
    BOOL _didAdjustContentViewHeight;
}
@end
@implementation MGModalView

#pragma mark - Override

- (void)dealloc {
    
}

- (instancetype)init {
    return [self initWithFrame:UIScreen.mainScreen.bounds];
}

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:UIScreen.mainScreen.bounds]) {
        self.backgroundColor = UIColor.clearColor;
        self.enableTapDismiss = YES;
        self.enableBackgroundUserInteraction = NO;
        self.animationDuration = 0.15;
        self.animationCurve = UIViewAnimationCurveEaseInOut;
        self.automaticallyAdjustsContentViewHeight = YES;
        
        _backgroundView = [[UIView alloc] initWithFrame:self.bounds];
        [_backgroundView setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.3]];
        [_backgroundView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onTapGesture:)]];
        [self addSubview:_backgroundView];
        
        _contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, 200)];
        [_contentView setBackgroundColor:UIColor.whiteColor];
        [self addSubview:_contentView];
    }
    return self;
}

- (MGModalType)preferredModalType {
    return MGModalTypeAlert;
}

- (CGSize)preferredContentSize {
    switch (self.preferredModalType) {
        case MGModalTypeAlert: return CGSizeMake(300, 200);
        case MGModalTypeAlertBottom:
        default:
            return CGSizeMake(self.width, 200);
    }
}

#pragma mark - Custom

- (void)adjustContentViewIfNeed {
    if (@available(iOS 11.0, *)) {
        if (self.automaticallyAdjustsContentViewHeight && self.preferredModalType == MGModalTypeAlertBottom && !_didAdjustContentViewHeight) {
            self.contentView.height += self.safeAreaInsets.bottom;
            _didAdjustContentViewHeight = YES;
        }
    }
}

- (void)clipCornerRadiusIfNeed {
    if (self.cornerRadius <= 0)
        return;
    
    if (self.preferredModalType == MGModalTypeAlertBottom) {
        CAShapeLayer *cornerLayer = [[CAShapeLayer alloc] init];
        CGSize cornerRadii = CGSizeMake(self.cornerRadius, self.cornerRadius);
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.contentView.bounds byRoundingCorners:UIRectCornerTopLeft|UIRectCornerTopRight cornerRadii:cornerRadii];
        cornerLayer.path = path.CGPath;
        self.contentView.layer.mask = cornerLayer;
    } else {
        self.contentView.layer.cornerRadius = self.cornerRadius;
        self.contentView.layer.masksToBounds = YES;
    }
}

- (void)presentInView:(UIView *)inView {
    [self setFrame:inView.bounds];
    [self.backgroundView setFrame:self.bounds];
    [inView addSubview:self];
    
    [self adjustContentViewIfNeed];
    [self clipCornerRadiusIfNeed];
    
    [self viewWillPresent];
    [self backgroundViewPresentAnimation];
    [self contentViewPresentAnimations:^{
        [self viewDidPresent];
    }];
}

- (void)dismiss {
    [self viewWillDismiss];
    [self backgroundViewDismissAnimation];
    [self contentViewDismissAnimations:^ {
        [self viewDidDismiss];
        [self removeFromSuperview];
    }];
}

- (void)viewWillPresent {}

- (void)viewDidPresent {
    _isPresented = YES;
}

- (void)viewWillDismiss {}

- (void)viewDidDismiss {
    _isPresented = NO;
    if (self.didDismissHandler) {
        self.didDismissHandler();
        self.didDismissHandler = nil;
    }
}

- (void)onTapGesture:(UITapGestureRecognizer *)gesture {
    if ([self enableTapDismiss]) {
        [self dismiss];
    }
}

#pragma mark - Animations

- (void)backgroundViewPresentAnimation {
    self.backgroundView.alpha = 0.0f;
    [UIView animateWithDuration:self.animationDuration animations:^{
        self.backgroundView.alpha = 1.0f;
    }];
}

- (void)backgroundViewDismissAnimation {
    [UIView animateWithDuration:self.animationDuration animations:^{
        self.backgroundView.alpha = 0.0f;
    }];
}
     
- (void)contentViewPresentAnimations:(void(^)(void))completion {
    switch (self.preferredModalType) {
        case MGModalTypeAlert:          [self contentViewPresentAlertAnimations:completion]; break;
        case MGModalTypeAlertBottom:    [self contentViewPresentActionSheetAnimations:completion]; break;
        case MGModalTypeCustom:         [self contentViewPresentCustomAnimation:completion]; break;
    }
}

- (void)contentViewPresentAlertAnimations:(void(^)(void))completion {
    self.contentView.center = self.center;
    self.contentView.alpha = 0.0;
    self.contentView.transform = CGAffineTransformMakeScale(1.2f, 1.2f);
    [UIView animateWithDuration:self.animationDuration delay:0 options:(self.animationCurve<<16) animations:^{
        self.contentView.alpha = 1.0;
        self.contentView.transform = CGAffineTransformMakeScale(1.0f, 1.0f);
    } completion:^(BOOL finished) {
        completion();
    }];
}

- (void)contentViewPresentActionSheetAnimations:(void(^)(void))completion {
    self.contentView.top = self.height;
    [UIView animateWithDuration:self.animationDuration delay:0 options:(self.animationCurve<<16) animations:^{
        self.contentView.bottom = self.height;
    } completion:^(BOOL finished) {
        completion();
    }];
}
     
- (void)contentViewDismissAnimations:(void(^)(void))completion {
    switch (self.preferredModalType) {
        case MGModalTypeAlert:          [self contentViewDismissAlertAnimations:completion]; break;
        case MGModalTypeAlertBottom:    [self contentViewDismissActionSheetAnimations:completion]; break;
        case MGModalTypeCustom:         [self contentViewDismissCustomAnimation:completion]; break;
    }
}

- (void)contentViewDismissAlertAnimations:(void(^)(void))completion {
    [UIView animateWithDuration:self.animationDuration delay:0 options:(self.animationCurve<<16) animations:^{
        self.contentView.transform = CGAffineTransformMakeScale(1.2f, 1.2f);
        self.contentView.alpha = 0.0f;
    } completion:^(BOOL finished) {
        completion();
    }];
}

- (void)contentViewDismissActionSheetAnimations:(void(^)(void))completion {
    [UIView animateWithDuration:self.animationDuration delay:0 options:(self.animationCurve<<16) animations:^{
        self.contentView.top = self.height;
    } completion:^(BOOL finished) {
        completion();
    }];
}
     
#pragma mark - 子类实现

/** 自定义present动画*/
- (void)contentViewPresentCustomAnimation:(void(^)(void))completion {
    completion();
}

/** 自定义dismiss动画*/
- (void)contentViewDismissCustomAnimation:(void(^)(void))completion {
    completion();
}

@end
